__author__ = 'nameoff'

# -*- coding: utf8 -*-
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from gaedjango.models import *
from utils import GetDirectLink, GetTweets
from parser import Parser,MultiThreadParser
from google.appengine.api import users

def HomePage(request):
	
	d = None
			
	try:
		d = request.GET['d']
		
	if d == '1':
		return HttpResponseRedirect(GetDirectLink(request.GET['l']))

	try:
		q = request.GET['q']
	except:
		return render_to_response('index.html')

	items = MultiThreadParser(q).startThreads()

	valid = 1
	if valid == 1:
		return render_to_response('index.html', {'res': items, 'q': q})
	else:
		return render_to_response('index.html', {'nothing': 1})

def AdmPage(request):
	
	user = users.get_current_user()
	
	if user:
		email = user.email()
	else:
		email = None
		
	login_url = users.create_login_url("/")
	logout_url = users.create_logout_url("/")
	
	return render_to_response('adm.html', {'user': user, 'email': email, 'login_url': login_url, 'logout_url': logout_url})
